package com.dinree.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.base.library.BaseActivity;
import com.dinree.R;
import com.dinree.databinding.ActivitySearchBinding;

public class SearchActivity extends BaseActivity {
    private ActivitySearchBinding activitySearchBinding;

    private String mKey;  //key值

    @Override
    public int getLayoutId() {
        return R.layout.activity_search;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        activitySearchBinding = getViewDataBinding();
        String url = getIntent().getStringExtra("url");
//        activitySearchBinding.webView.addJavascriptInterface(new AddressWebActivity.JSHook(), "dinree");
        activitySearchBinding.webView.loadUrl(url);
        activitySearchBinding.etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
                mKey = charSequence.toString();
                activitySearchBinding.webView.loadUrl("javascript:input('" + mKey + "')");
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        activitySearchBinding.etSearch.setOnKeyListener(new View.OnKeyListener() {

            @Override

            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if (keyCode == KeyEvent.KEYCODE_ENTER) {
                    // 先隐藏键盘
                    ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(SearchActivity.this.getCurrentFocus()
                                    .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                    activitySearchBinding.webView.loadUrl("javascript:search()");
                }
                return false;
            }
        });
        activitySearchBinding.tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }


}

